package com.fc.service.impl;

import java.util.List;

import org.hibernate.Criteria
import org.hibernate.Query
import org.hibernate.criterion.Projections
import org.hibernate.criterion.Restrictions
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional;

import com.fc.service.FoodService
import com.fc.entities.Food
import com.fc.entities.FoodGroup
import com.fc.entities.User
import com.fc.service.exceptions.FCException

/**
 * 
 * @author Ács Ádám
 * 
 */
@Repository("foodService")
@Transactional
class FoodServiceImpl extends BaseServiceImpl<Food> implements FoodService {
	@Override
	Food findByName(String name) {
		Criteria criteria = currentSession().createCriteria(Food.class)
		Food food = (Food) criteria.add(Restrictions.eq("name", name)).uniqueResult()
		return food
	}

	@Override
	List<String> findAllNames() {
		Criteria criteria = currentSession().createCriteria(Food.class, "food").setProjection(Projections.projectionList().add(Projections.property("food.name")))
		return criteria.list()
	}

	@Override
	List<Food> findAllByGroup(FoodGroup group) {
		Criteria criteria = currentSession().createCriteria(Food.class)
		return criteria.add(Restrictions.eq("foodGroup", group)).list()
	}

	@Override
	List<Food> findAllByUser(User user) {
		Criteria criteria = currentSession().createCriteria(Food.class)
		def foods = criteria.add(Restrictions.eq("user", user)).list()
		return foods
	}

	@Override
	List<Food> findAllByGroupAndUser(FoodGroup group, User user) {
		Criteria criteria = currentSession().createCriteria(Food.class)
		criteria.add(Restrictions.eq("foodGroup", group))
		criteria.add(Restrictions.eq("user", user))
		return criteria.list()
	}

	@Override
	List<Food> findAllDefault() {
		Query query = currentSession().createQuery("from Food where user_id = :userid ");
		query.setParameter("userid", "1");
		List list = query.list()
		return list
	}

	@Override
	List<Food> findAllDefaultByGroup(FoodGroup group) {
		Query query = currentSession().createQuery("from Food where user_id = :userid and foodGroup_id = :foodGroupid");
		query.setParameter("userid", "1");
		query.setParameter("foodGroupid", group.id);
		List list = query.list()
		return list
	}

	@Override
	void delete(Food food) {
		try {
			super.delete(food);
		}
		catch (Exception ex) {
			throw new FCException("Az adott étel (${food.name}) törlése nem lehetséges.")
		}
	}

	@Override
	Food save(Food food) {
		try {
			return super.save(food);
		}
		catch (Exception ex) {
			throw new FCException("Az adott étel (${food.name}) módosítása nem lehetséges.")
		}
	}

	@Override
	Food create(Food food) {
		try {
			return super.create(food);
		}
		catch (Exception ex) {
			throw new FCException("Az adott étel (${food.name}) létrehozása nem lehetséges.")
		}
	}

	@Override
	Food findById(long id) {
		find(Food.class, id)
	}

	@Override
	List<Food> findAll() {
		findAll(Food.class)
	}
}
